So now I think it's time to make our own weapon.
We're going to make this handgun here.
It shoots, it makes noise, it does damage.
There's no bells and whistles, no animations yet, no reload yet.
But we're going to get to that.
Pretty cool.
Let's go ahead and get started.
Let's go over to our tool box and see if we can get a mesh part that looks good enough for a gun that
we can use.
That way we don't have to weld a bunch of pieces together.
We don't want to use somebody else's model.
We want to do all the coding ourselves.
But the artwork is a different story because if you're like me, I can code really well, but I'm terrible
at art, so I'm going to look for a pistol.
Under Mashes meshes are a single part.
It's been something's been imported from like Blender or somebody created something really nice and
now it's just a single part with a mesh over top of it.
I'm going to drag that in that says Texture.
That looks great.
I think I'm going to work with this.
The gun is obviously too big.
Let's go ahead and resize it.
I'm going to close my tool box.
There we go.
We have more space.
Select the gun hit scale.
If it's not selected, you get those little dots.
Right.
And now we need to keep an eye on our size so when we resize it so mesh part is selected, that's the
gun.
Let's go down to size seven, 7.6.
That's the X.
So that's the thickness of the gun.
That should be about 0.25 studs, not seven and one half.
Hold the shift down, so everything shrinks maintaining the aspect ratio and then just drag this down
until that's about .25.
Uh, there you go.
Oh, look at that.
That's pretty good.
All right, now let's go to our workspace.
Hit the plus.
Add a tool.
So tools are special roadblocks, objects that the characters and players can use.
They can carry it in your hand.
Let's call that tool simple pistol, because a pistol is something we want to carry in our hand.
Let's grab the mesh part, which is the gun, drag it into the simple pistol.
Now all tools have to have a handle to pick it up.
I don't think I'm going to call my mesh part handled because then we have to work on the grip.
Let's just call the mesh part, gun part, and then I'm going to add a separate handle.
That way I can move it around and just eyeball it for how I want my grip.
I'm going to hit P for part.
There we go.
Make sure collisions are off.
We're going to put the part inside the gun.
Let's get our gun part.
Let me get the position.
I'll do C frame position.
Control C of the gun part.
Go to my part, part and then control V.
And now it's centered.
Let's change the size of our part.
My go to size, I'm going to make it kind of small.
But before we do that, let's see where the front of that part is.
I might add, the plus sign.
Add a decal.
So the front is actually pointing to the left.
We need the handle to be pointing in the same direction as the gun.
If we pick it up and we want it to be facing the right direction for our character, let's grab that
part.
And we can get rid of the cow.
It's going to move around like that if you hover over it.
We know that we need to change it so that it's facing the opposite direction.
There we go.
Now it's facing front.
Now I can change my size.
If it's too small, it's hard to see the decal.
That's why I wanted to do that first.
It's pretty small now.
Let's get our move.
I'm going to put.
This is going to be my handle.
I'm going to put it right where my hand is supposed to be.
On the gun.
Yeah, that's.
That's pretty good like that.
Now, let's rename it to handle.
It has to be spelled like that.
If it's not, it won't work.
Let's go to our simple pistol.
Hit the plus.
When I add this weld constraint to stick the handle on the gun part, so click.
The weld constraint hit part zero gun part.
Part one handle part.
And you could do it in any order.
All right, let's try and pick it up.
Let's do a play from here so you don't have to run around looking for that little gun.
And look at that.
Oh, that's pretty good, too.
I like that.
This is the one I had.
And this is the one I just did now.
All right.
Now I need a gun sound.
So let's go ahead and hit Stop.
And we got to find a bang for the gun.
Let's go to our toolbox and we'll do audio.
I'll say bang or let's just look for gun.
Gun.
GUNSHOT one was there.
Not bad.
Let's go ahead and click.
I'm going to put it on my gun part.
So we have we have distance roll off.
If you put sound in the workspace, if it's attached to an attachment or a bass part, you're going
to get a sound roll off with distance.
Let's do that insert.
So the gunshot is under the gun part.
I'm going to rename it to Bang.
Good.
And I can close this now.
I think we're set.
It's time to start scripting.
Let's go to our simple pistol, Hit the plus and add a local script.
The local script is going to be used to handle our mouse clicks and stuff when we're aiming.
So let's call that shoot.
Now you can't damage somebody or something on the server side with a local script, you need a server
script.
So go to simple pistol, hit the plus, hit an -- and then just get script.
That's a server script.
I'm going to call this damage, right?
We're going to probably do more than damage, but it's a good it's a good place to start.
You know, the damage is going to be a server script and now the shoot has to be able to talk to the
damage.
So what we're going to do is hit this plus hit an R and add a remote event.
Right.
Let's add that remote event.
I'll call it Shoot Ray.
Right.
For shoot remote event, double click on your shoot local script.
Make sure that's selected.
There it is.
Yeah.
Little computer there.
There we don't want the damage.
We want to do the local script.
First.
I'm going to make a variable called Gun Script Dot Parent.
So that's pointing to the tool, the simple pistol.
Now I want to get a variable for my remote event.
Shoot, Ray, It's on the gun, but we're going to have to do a wait for child, because if it's in
our backpack, we could run into trouble.
Shoot, Ray.
We'll just wait for it.
All right?
I need a variable for my player.
I will initialize that to nil.
I need a variable for my mouse.
Also initialize that to nil and my connection.
You're going to see what that's for.
But it's nil right now.
Now I need a couple of functions.
I need a function for activated.
When I go to click on something, I'm going to call that on activated.
And this is where we're going to fire our remote event.
So we'll have our shoot.
Ray Colon Fire Server.
So we did a fire client, but now we're doing a fire server we're going to send.
Well, I forgot a plus sign or an equal sign right here.
There we go.
We are going to send the mouse target.
If we have one mouse dot target, it could be nil.
I know we haven't gotten the mouse yet, but that's all right.
Don't worry.
We're going to get to that.
We're going to do local function on equipped when we put the gun on.
That's where we're going to get our player.
So player will then equal game dot players.
Local player.
We can do local player because this is a local script, but we're waiting before we put it on, right
before we equip it, before we try to get it.
Now we're going to get a mouse from the player, player Colon Get mouse.
That's pretty convenient.
Now we're going to do something a little bit different.
This connection variable is actually going to point to my activated event.
We're going to connect this to our on activate function.
If you get two extra parentheses in there, get rid of that.
We before have been doing our events just by putting it down here, making the connection, but now
we're going to connect and disconnect.
So we're going to use a variable a little bit different.
I'm going to use almost the same structure for my unequipped, so I'm copying this to a control C control
V and this is going to be on unequipped.
And then what I'm going to do is I'm going to get my player, I'm going to make it nil, I'm going to
get my mouse, I'll make that nil and my connection.
Not Nell, but Colon.
Disconnect.
So this will clean up some resources.
If you have a lot of people running around with guns, you might start chewing up some resources with
that.
Now, down here, we're going to get the gun and we have a quipped event that happens when you pick
something up.
That's a tool and we'll connect that to on equipped.
We're going to get a tool dot unequipped event connect that to on unequipped.
There it is and then our activated will be connected when we put it on.
Now we need to work on our damage script.
So if you recall, we have this on activated function that when we click on something, this will go
off and then we're going to use a remote event to fire to the server, the mouse target.
We're going to catch that in the damage script.
So let's go to Damage or click on it here.
If you didn't remember where you put it under the pistol, under the simple pistol, let's get a variable
for our shoot re script Dot parent Wait for child shoot.
Ray Let's get a variable for damage and we'll make it pretty weak so we can get like mods and stuff
with our points and then we need to bang the sound script.
Dot parent.
It's on the gun part.
Remember we put it on the gun part so we'd get some sound roll off.
There it is.
Bang.
So we'll do a dot bang.
I need a function local function on shoot.
We are going to get the player.
I know that we didn't pass the player, but since this is coming from a fire server on the client side,
the player will get passed first no matter what.
Then we're also going to get a target.
That's the thing we clicked on.
We pass that over.
No matter now, the target might be nil.
Whether is or not doesn't matter.
We're going to do the bang play.
We're going to make the sound because guns go off.
Even if they don't hit anything.
We'll do an if statement to check to see if the target is not nil.
So if the target exists, that's the same as if not nil.
Right.
Just so you recall and I'm also going to check to see if the target parent exists, because sometimes
when things are dying, the characters removed before a body part, sometimes you get an error.
Just being careful.
I did my bang.
I'm check my target.
Let's check for a humanoid to see if we can do some damage.
So if the humanoid exists, we're going to do target dot parent We know that exists.
Find first child That's a humanoid.
If there is a humanoid underneath the parent of the target, we better check though because there might
not be.
Then we can do HUME Colon take damage and then we'll pass in our damage amount.
Now, this isn't going to go off unless we catch that remote event, but we can do that easy enough.
We're going to say shoot our dot on server event because we're we're on the server side with our damage
script, connect on shoot, get rid of those two extra parentheses that Roblox likes to put in there.
You only want the function name when you're connecting it.
All right, let's try it.
Let's go to our world.
Oh, and the gun is floating around out there.
Let's get the gun and let's move it down to our starter pack.
Their starter pack that was going to be in our backpack when we start our game.
I should not play from here.
I'm going to have to run for something.
That's okay.
We can get a coin, right?
Here's our pistol.
There it is.
Does it make noise?
Does it give us an error?
Nope.
Oh, there's a coin.
We'll go run for one of those zombies.
There's a zombie.
All right.
Looking good and no errors.
So we'll have to add a little kickback function to our to our pistol.
We're going to keep track of kills with the pistol, at least player kills.
We're going to add points for NPC kills, and then we've got to do reload animation.
Got a lot of cool stuff that goes along with making a gun.
I will see you in the next video.